﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.CM.Controllers
{
    public class RefuelController : Controller
    {
        //
        // GET: /CM/Refuel/

        public ActionResult RefuelList(string PassCenterCode, string PassStationCode, string PassPlateNumber, string PassCarNumber)
        {           
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            this.ViewData["em"] = em;
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            this.ViewData["selfWorkCode"] = selfWorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            this.ViewData["selfCenterID"] = selfCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            this.ViewData["selfStationID"] = selfStationID;
            //begin 如果是机动班，就将查全部的最大权限给他  20160901 yh
            if (selfStationID=="001")
            {
                this.ViewData["em"] = E_StatisticsPermisson.ALL;
            }
            //end
            UserOperateContext userOperateContext = new UserOperateContext();
            bool Insert = userOperateContext.getGongneng(1260);//判断登录人有无“新增”油卡加油权限
            if (Insert)
            {
                this.ViewData["RolePermissionInsert"] = "Insert";//登录人有新增油卡加油权限
            }
            bool Edit = userOperateContext.getGongneng(1317);//判断登录人有无油卡加油修改权限
            if (Edit)
            {
                this.ViewData["RolePermissionEdit"] = "Edit";//登录人有油卡加油修改权限
            }
            this.ViewData["start"] = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd 00:00:00");
            this.ViewData["end"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
            this.ViewData["name"] = UserOperateContext.Current.Session_UsrInfo.Name;
            this.ViewData["time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (PassCenterCode != "--请选择--" && PassCenterCode != "" && PassCenterCode != null)
            { this.ViewData["txtCenterCode"] = PassCenterCode; };
            if (PassStationCode != "--请选择--" && PassStationCode != "" && PassStationCode != null)
            { this.ViewData["txtStationCode"] = PassStationCode; };
            if (PassPlateNumber != "--请选择--" && PassPlateNumber != "" && PassPlateNumber != null)
            { this.ViewData["txtPlateNumber"] = PassPlateNumber; };
            if (PassCarNumber != "--请选择--" && PassCarNumber != "" && PassCarNumber != null)
            { this.ViewData["txtCarNumber"] = PassCarNumber; };
            return View();
        }

        /// <summary>
        /// 查询加油列表信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DataLoad()
        {
            try
            {
                string start = Request.Form["start"].ToString();
                string end = Request.Form["end"].ToString();
                string center = Request.Form["center"].ToString();
                string station = Request.Form["station"].ToString();
                string actualCode = Request.Form["actualCode"].ToString();
                string ambulanceNumber = Request.Form["ambulanceNumber"].ToString();
                string oilType = Request.Form["oilType"].ToString();
                string shift = Request.Form["shift"].ToString();
                string cardCode = Request.Form["cardCode"].ToString();
                //获取页容量
                int rows = int.Parse(Request.Form["rows"]);
                //获取请求的页码
                int page = int.Parse(Request.Form["page"]);
                E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();               
                string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                string selfName = UserOperateContext.Current.Session_UsrInfo.Name;       
                string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;      
                string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;

                var list = new C_OilCardRefuelBLL().GetRefuelList(page, rows, start, end, center, station, cardCode, actualCode, ambulanceNumber
                                                                    , oilType, shift, em, selfWorkCode,selfName, selfCenterID, selfStationID);
                JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
                return j;
            }

            catch (Exception e)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("InfoID", "0");
                dict.Add("InfoMessage", e.Message);
                return this.Json(dict);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RefuelEdit()
        {
            string sDetailInfo = Request.Form["sRefuelInfo"].ToString();
            C_OilCardRefuel model = JsonHelper.GetJsonInfoBy<C_OilCardRefuel>(sDetailInfo);
            int sid = Convert.ToInt32(model.ID);
            //通过sid查询出此条数据原来的所有值
            C_OilCardRefuel oldModel = new C_OilCardRechargeBLL().GetOldModel(sid);
            model.FillTime = DateTime.Now;
            string centerID = new C_OilCardRechargeBLL().GetCenterID(model.CardCode);
            string stationID = new C_OilCardRechargeBLL().GetStationID(model.CardCode);
            model.CenterID = Convert.ToInt32(centerID);
            model.StationID = stationID;
            //油卡加油修改后，油卡基础表中卡内金额自动更新
            //通过油卡卡号查出基础表中此卡对应的信息
            C_OilCardInfo info = new C_OilCardRechargeBLL().GetOilCardList(model.CardCode);
            int res = new C_OilCardRefuelBLL().Modify(model,"CenterID", "StationID", "CardCode","ActualCode","AmbulanceNumber", "AmbulanceCode", "OilType", "OilMass",
                                                            "LastGasPathCode", "GasPathCode", "DrivingKM", "DriverWorkID", "DriverName", "RefuelTime","FillTime",
                                                            "Remark","bakup1","bakup2","ProofNumber","RefuelPerson","RefuelMoney","FillPerson", "Shift");
            if (res > 0)
            {
                //只有修改卡内金额和加油金额时卡内余额才会更新到油卡信息基础表中 2016-09-05 yh
                if (oldModel.bakup1 != model.bakup1 || oldModel.RefuelMoney != model.RefuelMoney || oldModel.bakup2 != model.bakup2)
                {
                    info.bak1 = model.bakup2;
                    int ress = new C_OilCardInfoBLL().Modify(info, "bak1");
                }

                return this.JsonResult(Utility.E_JsonResult.OK, "修改成功！", null, null);
            }
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "修改失败", null, null);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RefuelAdd()
        {
            string sDetailInfo = Request.Form["sRefuelInfo"].ToString();
            C_OilCardRefuel info = JsonHelper.GetJsonInfoBy<C_OilCardRefuel>(sDetailInfo);
            string centerID = new C_OilCardRechargeBLL().GetCenterID(info.CardCode);
            string stationID = new C_OilCardRechargeBLL().GetStationID(info.CardCode);
            info.CenterID = Convert.ToInt32(centerID);
            info.StationID = stationID;
            info.FillTime = DateTime.Now;
            if (info.AmbulanceNumber == null)
            { info.AmbulanceNumber = ""; }
            List<C_OilCardRefuel> list = new C_OilCardRefuelBLL().GetMaxID();
            if (list.Count == 0)
            { info.ID = 1; }
            else
            { info.ID = list[0].ID + 1; }
            //油卡加油后，油卡基础表中卡内金额自动更新
            //通过油卡卡号查出基础表中此卡对应的信息
            C_OilCardInfo model = new C_OilCardRechargeBLL().GetOilCardList(info.CardCode);
            int res = new C_OilCardRefuelBLL().Add(info);
            if (res > 0)
            {
                model.bak1 = info.bakup2;
                int ress = new C_OilCardInfoBLL().Modify(model, "bak1");
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            }
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
        }

        //通过选择车牌号码查出车辆编号和车辆编码,所属分站，分中心，最近（ID最大）的加油时间和本次加油路码
        //和最近（ID最大）的整修保养时间，进厂里程表数，出厂里程表数，是否更改路码
        [HttpPost]
        public ActionResult GetListByActualCode(string actualCode)
        {
            C_OilCardLink model = new C_OilCardRefuelBLL().GetActualCodeLinkList(actualCode);
            return Json(new { model }, "appliction/json", JsonRequestBehavior.AllowGet);
        }
        //通过选择车辆编号查出车牌号码和车辆编码，所属分站，分中心，最近（ID最大）的加油时间和本次加油路码
        //和最近（ID最大）的整修保养时间，进厂里程表数，出厂里程表数，是否更改路码
        [HttpPost]
        public ActionResult GetListByAmbulanceNumber(string ambulanceNumber)
        {
            C_OilCardLink model = new C_OilCardRefuelBLL().GetAmbulanceNumberLinkList(ambulanceNumber);
            return Json(new { model }, "appliction/json", JsonRequestBehavior.AllowGet);
        }

        // 获取车牌号码(有效且没报废的车)    
        [HttpPost]
        public ActionResult GetActualCode(E_StatisticsPermisson em, string selfWorkCode, string selfCenterID, string selfStationID)
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetActualCodeList(em, selfWorkCode, selfCenterID, selfStationID);
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取车牌号码(所有)    
        [HttpPost]
        public ActionResult GetAllActualCode()
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetAllActualCodeList();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        // 获取车辆编号(有效且没报废的车)    
        [HttpPost]
        public ActionResult GetAmbulanceNumber(E_StatisticsPermisson em, string selfWorkCode, string selfCenterID, string selfStationID)
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetAmbulanceNumberList(em, selfWorkCode, selfCenterID, selfStationID);
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取车辆编号(所有)    
        [HttpPost]
        public ActionResult GetAllAmbulanceNumber()
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetAllAmbulanceNumberList();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取加油油号    
        [HttpPost]
        public ActionResult GetOilType()
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetOilTypeList();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取班次
        [HttpPost]
        public ActionResult GetShift()
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetShiftList();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取修理类别
        [HttpPost]
        public ActionResult GetRepairType()
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetRepairTypeList();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取加油人
        [HttpPost]
        public ActionResult GetRefuelPerson()
        {
            try
            {
                C_OilCardRefuelBLL bll = new C_OilCardRefuelBLL();
                var result = new object();
                result = bll.GetRefuelPersonList();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
    }
}
